home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-02
/
pas_0593.zip
/
SORTQASM.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1993-05-30
|
2KB
|
100 lines
{─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
Msg : 494 of 527
From : Alexander Christov 2:341/34.0 14 May 93 12:43
To : All
Subj : Sorting Routines (3/3)
────────────────────────────────────────────────────────────────────────────────
Hi All!}
{ And finally a specific version of QSort() written in Assembler. It is
non recursive and sorts arrays of WORDs of up to 16383 elements (since
it uses the addresses of the elements rather than their indexes, and since
SizeOf(Word)=2 -> 16384*2=32768 "=" -32768, and the routine uses signed
comparisons between adresses.
On my 386/33 it sorts 10 times an array of 10000 words in 3.6 sec, while
the first QSort() does the same in 46 sec.
Must be called with
Qsort(pointer to the first element, 0, elements-1)
Use freely. If you include the source directly in your program, credit
must be given.
}
Procedure QSort(Base:Pointer;L,R:Word);Assembler;
VAR TmpL,TmpR,TmpDI:WORD;
ASM
XOR AX,AX
PUSH AX
PUSH AX { 0 0 will act as a flag on the stack indicating that no more }
PUSH R { (L,R) pairs need to be sorted }
PUSH L
@MainLoop:
LES DI,Base
MOV TmpDI,DI
XOR SI,SI
MOV BX,DI
POP AX { AX<-L }
MOV TmpL,AX
MOV SI,AX
SHL AX,1
ADD DI,AX
POP AX { AX<-R }
MOV TmpR,AX
AND AX,AX { R can be never 0 except if this is the (0,0) flag }
JZ @End
ADD SI,AX
SHL AX,1
ADD BX,AX
AND SI,$FFFE
ADD SI,TmpDI
{ ES:DI -> Element[I] (L)
ES:BX -> Element[J] (R)
ES:SI -> Element[(L+R) div 2]
}
MOV AX,ES:[SI]
@Loop1:
MOV CX,ES:[DI]
CMP AX,CX
JNA @Loop2
ADD DI,2
JMP @Loop1
@Loop2:
MOV CX,ES:[BX]
CMP CX,AX
JNA @Check
SUB BX,2
JMP @Loop2
@Check:
CMP DI,BX
JG @Cont1
MOV CX,ES:[DI]
MOV DX,ES:[BX]
MOV ES:[DI],DX
MOV ES:[BX],CX
ADD DI,2
SUB BX,2
CMP DI,BX
JNG @Loop1
@Cont1:
SUB DI,TmpDI
SAR DI,1 { DI - I }
SUB BX,TmpDI
SAR BX,1 { BX - J }
CMP DI,TmpR
JGE @Cont2
PUSH TmpR { I<R }
PUSH DI
@Cont2:
CMP TmpL,BX
JGE @MainLoop
PUSH BX { L<J }
PUSH TmpL
JMP @MainLoop
@End:
END;